package org.androidpn.server.xmpp.packet;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.server.entity.FriendMessage;
import org.jivesoftware.smack.util.StringUtils;
import org.xmpp.packet.IQ;

/**
 * packet 好友消息
 * 
 * @author 蔡琪
 * 
 */
public class FriendMessagePacket extends IQ {

	/**
	 * 操作枚举
	 * 
	 * @author 蔡琪
	 * 
	 */
	public enum Operation {
		/** 发送消息 */
		sendMessage,
		/** 接收消息 */
		getMessage,
		/** 推送消息 */
		pushMessage
	}

	/** 好友房间消息列表 */
	private List<FriendMessage> friendMessages = new ArrayList<FriendMessage>();
	/** 游戏房间消息 */
	private FriendMessage friendMessage;
	/** 角色id */
	private Long roleId;
	/** 操作信息 */
	private Operation operation;

	public List<FriendMessage> getFriendMessages() {
		return friendMessages;
	}

	public void setFriendMessages(List<FriendMessage> friendMessages) {
		this.friendMessages = friendMessages;
	}

	public FriendMessage getFriendMessage() {
		return friendMessage;
	}

	public void setFriendMessage(FriendMessage friendMessage) {
		this.friendMessage = friendMessage;
	}

	public Long getRoleId() {
		return roleId;
	}

	public void setRoleId(Long roleId) {
		this.roleId = roleId;
	}

	public Operation getOperation() {
		return operation;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

	public String getChildElementXML() {
		StringBuffer xml = new StringBuffer();
		xml.append("<query xmlns=\"jabber:iq:friendMessage\">");
		xml.append("<friendMessages>");
		for (FriendMessage friendMessage : friendMessages) {
			xml.append(createFriendMessageNode(friendMessage));
		}
		xml.append("</friendMessages>");
		if (friendMessage != null) {
			xml.append(createFriendMessageNode(friendMessage));
		}
		if (roleId != null) {
			xml.append("<roleId>");
			xml.append(roleId);
			xml.append("</roleId>");
		}
		if (operation != null && !operation.equals("")) {
			xml.append("<operation>");
			xml.append(operation);
			xml.append("</operation>");
		}
		xml.append("</query>");
		return xml.toString().replaceAll("\\>\\s*|\t|\r|\n", ">");
	}

	/**
	 * 生成friendMessage节点
	 * 
	 * @param friendMessage
	 * @return
	 */
	private String createFriendMessageNode(FriendMessage friendMessage) {
		return "<friendMessage>"
				+ StringUtils.escapeNode(friendMessage.toXML())
				+ ("</friendMessage>");
	}

}
